package cn.thr.ymcc.consumer;

import cn.thr.ymcc.constant.Cos;
import cn.thr.ymcc.dto.PaySuccessDto;
import cn.thr.ymcc.service.ICourseOrderService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Component
@Slf4j
@RocketMQMessageListener(
        consumerGroup = "PaySuccessHandler",
        topic = Cos.pay.TOPIC_PAY_SUCCESS,
        selectorExpression = Cos.pay.TAGS_PAY_SUCCESS,
        //广播模式
        messageModel = MessageModel.BROADCASTING
)
public class PaySuccessHandler implements RocketMQListener<MessageExt> {

    @Autowired
    private ICourseOrderService courseOrderService;

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String jsonStr = new String(body, "utf-8");
            log.info("订单服务中正在处理支付成功的业务。。。  {}",jsonStr);
            //将json字符串转换为对象
            PaySuccessDto paySuccessDto = JSON.parseObject(jsonStr, PaySuccessDto.class);
            //service 处理支付成功  修改订单状态为已支付
            courseOrderService.paySuccess(paySuccessDto);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
